package com.example.a2306_shop.address

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.a2306_shop.R
import com.example.a2306_shop.databinding.ActivityAddressEditBinding
import com.example.lib_base.mvvm.BaseActivity
import com.example.lib_base.param.Address
import com.example.lib_base.param.AddressEdit
import com.github.gzuliyujiang.wheelpicker.AddressPicker
import com.github.gzuliyujiang.wheelpicker.annotation.AddressMode

@Route(path = "/app/AddressEditActivity")
class AddressEditActivity : BaseActivity<ActivityAddressEditBinding,AddressViewModel>() {
    /**
     * autowired 不用添加name  原因：两边名称一致
     * jvmField  将字段变成 公开的，才能进行调用
     * var  因为默认值是不正确 我们需要根据参数进行修改
     */
    @Autowired
    @JvmField
    var addressId = 0
    lateinit var address: Address
    override fun initData() {
        viewModel.addressDetailSuccess.observe(this){
            if(it.code==200){
                binding.etName.setText(it.data.realName)
                binding.etPhone.setText(it.data.phone)
                binding.tvAddress.setText(it.data.province+it.data.city+it.data.district)
                binding.etDetail.setText(it.data.detail)
                binding.cb.isChecked = it.data.isDefault

                //给address进行赋值 防止用户不再次点击地址
                address.province = it.data.province
                address.city = it.data.city
                address.district = it.data.district
            }else{
                ToastUtils.showLong(it.message)
            }
        }
        viewModel.addressEditSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                finish()
            }
        }
    }

    override fun initView() {
        ARouter.getInstance().inject(this)//一定写在优先调用的方法中  initView()
        //请求详情接口  展示详细信息
        viewModel.addressDetail(addressId)

        address = Address()
        //点击弹出地址框
        binding.tvAddress.setOnClickListener {
            val picker = AddressPicker(this)
            picker.setTitle("请选择配送地址")
            picker.setAddressMode(AddressMode.PROVINCE_CITY_COUNTY)
            picker.setOnAddressPickedListener { province, city, county ->
                binding.tvAddress.text = "${province.name}${city.name}${county.name}"
                address.province = province.name
                address.city = city.name
                address.district = county.name
            }
            picker.show()
        }
        //点击保存,调用接口
        binding.btnSave.setOnClickListener {
            //所有均需必填
            val addressEdit = AddressEdit()
            addressEdit.realName = binding.etName.text.toString()//姓名
            addressEdit.phone = binding.etPhone.text.toString()//手机号
            addressEdit.detail = binding.etDetail.text.toString()//详细地址
            addressEdit.isDefault = binding.cb.isChecked//是否选中设为默认地址
            addressEdit.id = addressId//新增不需要给  是自增生成的  修改  需要给 ，要修改指定id的数据

            addressEdit.address = address//具体地址
            viewModel.addressEdit(addressEdit)
        }

    }

    override fun getViewModel(): Class<AddressViewModel>  = AddressViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_address_edit

}